import WebSocketClient from '@/utils/websocket'
import {chat} from '@/api/protobuf-loader'

const ws = new WebSocketClient('/ws')

let handler = {};


export function addHandler(event, handler) {
    handler[event] = handler;
}


// 发送文本消息
export const sendTextMessage = async (request_id, from_id, session_id, text) => {
    const msg = new chat.Message();
    msg.setMessageId(request_id); // 请求id作为messageid，服务端会返回一个messageid
    msg.setFromId(from_id);
    msg.setSessionId(session_id);
    msg.setText(text);
    const wsMsg = new chat.WsMessage(); 
    wsMsg.setType(chat.Type.message);
    wsMsg.setMessage(msg);
    ws.send(wsMsg.serializeBinary());
}



// 发送图片消息
export const sendImageMessage = async(
    request_id, 
    from_id, 
    session_id, 
    image_id, 
    image_content
) => {
    const msg = new chat.Message();
    msg.setMessageId(request_id); // 请求id作为messageid，服务端会返回一个messageid
    msg.setFromId(from_id);
    msg.setSessionId(session_id);
    
    // 创建ImageInfo对象并设置属性
    const imageInfo = new chat.ImageInfo();
    imageInfo.setId(image_id);
    imageInfo.setPath(image_content); // 使用setPath而不是setContent，因为ImageInfo只有id和path属性
    
    // 将ImageInfo对象设置到Message中
    msg.setImage(imageInfo);
    
    const wsMsg = new chat.WsMessage(); 
    wsMsg.setType(chat.Type.message);
    wsMsg.setMessage(msg);
    ws.send(wsMsg.serializeBinary());
}

export const sendMessageReadAck = async(
    from_id, 
    session_id, 
    message_id
) => {
    const msg = new chat.ReadACK();
    msg.setUserId(from_id);
    msg.setSessionId(session_id);
    msg.setMessageId(message_id);
    const wsMsg = new chat.WsMessage(); 
    wsMsg.setType(chat.Type.read_ack);
    wsMsg.setReadAck(msg);
    console.log('sendMessageReadAck', wsMsg.toObject());
    ws.send(wsMsg.serializeBinary());
}



function RecvMessage(wsMsg) {
    const msg = wsMsg.getMessage();
    const message = {
        message_id: msg.getMessageId(),
        from_id: msg.getFromId(),
        session_id: msg.getSessionId(),
        sent_at: msg.getSentAt(),
        text: msg.getText(),
        image: {
            id: msg.getImage().getId(),
            content: msg.getImage().getContent(),
        }
    }
    handler["message"](message);
}
function RecvSendACK(wsMsg) {
    const ack = wsMsg.getSendAck();
    const send_ack = {
        request_id: ack.getRequestId(),
        session_id: ack.getSessionId(),
        message_id: ack.getMessageId(),
        send_at: ack.getSendAt(),
    }
    handler["send_ack"](send_ack);
}
function RecvReadACK(wsMsg) {
    const ack = wsMsg.getReadAck();
    const read_ack = {
        session_id: ack.getSessionId(),
        message_id: ack.getMessageId(),
    }
    handler["read_ack"](read_ack);
}

const callback = new Map();
callback.set(String(chat.Type.message), RecvMessage);
callback.set(String(chat.Type.send_ack), RecvSendACK);
callback.set(String(chat.Type.read_ack), RecvReadACK);

ws.on('message', (event) => {
    // 调用合适的回调函数
    const bytes = new Uint8Array(event.data);
    const wsMsg = chat.WsMessage.deserializeBinary(bytes);
    const type = String(wsMsg.getType());
    const cb = callback.get(type);
    if (cb) {
        cb(wsMsg);
    }
})